package frontend.controller;

import entities.*;
import frontend.entity.Day_Data;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@Controller
public class WebController{
    @Resource
    private RestTemplate restTemplate;

    public static final String PRODUCER_URL="http://localhost:8001";

    @RequestMapping("/")
    public String index(){return "index";}

    @RequestMapping("/chinaMap")
    public String chinaMap(){
        return "chinaMap";
    }
    @RequestMapping("/love")
    public String love(){
        return "love";
    }

//    详情页，展示各省份详情数据
    @RequestMapping("/detail")
    public String detail(String province, Model model){
        model.addAttribute("provinceName",province);
        return "detail";
    }

//    各省份的地图数据
    @RequestMapping("/detailData")
    @ResponseBody
    public MapData[] detailData(String provinceName){
        return restTemplate.getForObject(PRODUCER_URL+"/detailData?provinceName="+provinceName,MapData[].class);
    }

    @RequestMapping("/get_total_cases")
    @ResponseBody
    public TotalCases get_total_cases(){
        return restTemplate.getForObject(PRODUCER_URL + "/get_total_cases",TotalCases.class);
    }

    @RequestMapping("/mapData")
    @ResponseBody
    public MapData[] getMapData(){
        return restTemplate.getForObject(PRODUCER_URL+"/getMapData",MapData[].class);
    }

    @RequestMapping("/getAll")
    @ResponseBody
    public ProvinceAndCity[] getAll(){
        return restTemplate.getForObject(PRODUCER_URL + "/getAll",ProvinceAndCity[].class);
    }

    @RequestMapping("/getEveryDay")
    @ResponseBody
    public EveryDay[] getEveryDay(){
        return restTemplate.getForObject(PRODUCER_URL + "/getEveryDay",EveryDay[].class);
    }

    //    省份对应城市子列表
    @RequestMapping("/getProvince")
    @ResponseBody
    public ProvinceAndCity[] getProvince(String proName){
        return restTemplate.getForObject(PRODUCER_URL + "/getProvince"+"?proName="+proName,ProvinceAndCity[].class);
    }

    @RequestMapping("/getProvinceDay")
    @ResponseBody
    public Day_Data[] getProvince(){
        Day_province[] day_pro_str = restTemplate.getForObject(PRODUCER_URL + "/getProvinceDay", Day_province[].class);
        Day_Data[] array=new Day_Data[day_pro_str.length];
        for (int j=0;j<day_pro_str.length;j++) {
            String data = day_pro_str[j].getData();
            String[] data_str = data.split(",");
            Integer[] data_int=new Integer[data_str.length];
            for(int i=0;i<data_str.length;i++){
                data_int[i]=Integer.parseInt(data_str[i]);
            }
            array[j]=new Day_Data(day_pro_str[j].getDate(),data_int);
        }
        return array;
    }
}
